
function dump(x,y)
  	if type(x) ~= "table" then
    	error("expected table and string, but got " .. type(x) .. " and " ..type(y), 2);
  	end

  	for k,v in pairs(x) do
    	-- if a type is specified, check to see
      	-- if current value is of right type.
      	if y ~= nil then
        	if type(v) == y then
            	print(k, "\t", v);
          	end
      	else
        	print(k, "\t", v)
      	end
  	end
end
function globaldump(type)
  	print("Current Lua Memory Usage: "..collectgarbage("count").." Kilobytes\n");
  	dump(_G, type);
end

-- setup the sandbox environment
local sandbox = {}
sandbox._G = sandbox
sandbox.print=print
sandbox.register_default_modules = register_default_modules
sandbox.collectgarbage=collectgarbage
sandbox.type=type
sandbox.assert=assert
sandbox.pairs=pairs
sandbox.ipairs=ipairs
sandbox.error=error
sandbox.next=next
sandbox.select=select
sandbox.tonumber=tonumber
sandbox.tostring=tostring
sandbox.pcall=pcall
sandbox.xpcall=xpcall

sandbox.unpack=unpack
sandbox._VERSION=_VERSION
sandbox.coroutine = { 	create = coroutine.create,
				  		resume = coroutine.resume,
				  		running = coroutine.running,
				  		status = coroutine.status,
				  		wrap = coroutine.wrap,
				  		yield = coroutine.yield
					}
sandbox.package = {	loaded = {}
				  }
sandbox.math = 	{
					abs = math.abs,
					acos = math.acos,
					asin = math.asin,
					atan = math.atan,
					atan2 = math.atan2,
					ceil = math.ceil,
					cos = math.cos,
					cosh = math.cosh,
					deg = math.deg,
					exp = math.exp,
					floor = math.floor,
					fmod = math.fmod,
					frexp = math.frexp,
					huge = math.huge,
					ldexp = math.ldexp,
					log = math.log,
					log10 = math.log10,
					max = math.max,
					min = math.min,
					modf = math.modf,
					pi = math.pi,
					pow = math.pow,
					rad = math.rad,
					random = math.random,
					randomseed = math.randomseed,
					sin = math.sin,
					sinh = math.sinh,
					sqrt = math.sqrt,
					tan = math.tan,
					tanh = math.tanh
				}
sandbox.table = {
					insert = table.insert,
					maxn = table.maxn,
					remove = table.remove,
					sort = table.sort
				}
sandbox.string = {
					byte = string.byte,
					char = string.char,
					find = string.find,
					format = string.format,
					gmatch = string.gmatch,
					gsub = string.gsub,
					len = string.len,
					lower = string.lower,
					match = string.match,
					rep = string.rep,
					reverse = string.reverse,
					sub = string.sub,
					upper = string.upper
				}
sandbox.os = 	{
					clock = os.clock,
					date = os.date,
					difftime = os.difftime,
					time = os.time
				}
register_default_modules(sandbox)
register_default_modules = nil		-- ensure that the reference to managed code disapears


function ExecuteUnsafeCode(untrusted_code)
	local untrusted_function, message = loadstring(untrusted_code)
	if not untrusted_function then 
  		print(message);
  		return
  	end
  
  	setfenv(untrusted_function, sandbox);
  	pcall(untrusted_function);

	-- these are the functions we are interested in learning about
	if sandbox.initialize then
		initialize = sandbox.initialize;
	end

	if sandbox.event then
		event = sandbox.event;
	end

end
